package com.suao.fruits.service.impl;

import com.suao.common.constant.DelFlagConst;
import com.suao.common.utils.DateUtils;
import com.suao.common.utils.SecurityUtils;
import com.suao.common.utils.StringUtils;
import com.suao.fruits.domain.FruitsInfo;
import com.suao.fruits.domain.FruitsSpoilageInfo;
import com.suao.fruits.mapper.FruitsSpoilageInfoMapper;
import com.suao.fruits.service.IFruitsInfoService;
import com.suao.fruits.service.IFruitsSpoilageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 果店损耗管理Service业务层处理
 *
 * @author tyz
 * @date 2023-03-08
 */
@Service
public class FruitsSpoilageInfoServiceImpl implements IFruitsSpoilageInfoService
{
    @Autowired
    private FruitsSpoilageInfoMapper fruitsSpoilageInfoMapper;
    
    @Autowired
    private IFruitsInfoService fruitsInfoService;

    /**
     * 查询果店损耗管理
     *
     * @param id 果店损耗管理主键
     * @return 果店损耗管理
     */
    @Override
    public FruitsSpoilageInfo selectFruitsSpoilageInfoById(Long id)
    {
        return fruitsSpoilageInfoMapper.selectFruitsSpoilageInfoById(id);
    }

    /**
     * 查询果店损耗管理列表
     *
     * @param fruitsSpoilageInfo 果店损耗管理
     * @return 果店损耗管理
     */
    @Override
    public List<FruitsSpoilageInfo> selectFruitsSpoilageInfoList(FruitsSpoilageInfo fruitsSpoilageInfo)
    {
        return fruitsSpoilageInfoMapper.selectFruitsSpoilageInfoList(fruitsSpoilageInfo);
    }

    /**
     * 新增果店损耗管理
     *
     * @param fruitsSpoilageInfo 果店损耗管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFruitsSpoilageInfo(FruitsSpoilageInfo fruitsSpoilageInfo)
    {
        
        fruitsSpoilageInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsSpoilageInfo.setCreateTime(DateUtils.getNowDate());
        fruitsSpoilageInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsSpoilageInfo.setUpdateTime(DateUtils.getNowDate());
        int result = fruitsSpoilageInfoMapper.insertFruitsSpoilageInfo(fruitsSpoilageInfo);
        if(result>0)
        {
            //入库 增加库存
            FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(fruitsSpoilageInfo.getFruitsId());
            if(StringUtils.isNotNull(fruitsInfo))
            {
                BigDecimal lossNum = fruitsInfo.getLossNum().add(fruitsSpoilageInfo.getSpoilageNum());  //损耗数量+
                BigDecimal inventory = fruitsInfo.getInventory().subtract(fruitsSpoilageInfo.getSpoilageNum());   //可用库存 -
                fruitsInfo.setLossNum(lossNum);
                fruitsInfo.setInventory(inventory);
            }
            fruitsInfoService.updateFruitsInfo(fruitsInfo);
        }
        return result;
    }

    /**
     * 修改果店损耗管理
     *
     * @param fruitsSpoilageInfo 果店损耗管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFruitsSpoilageInfo(FruitsSpoilageInfo fruitsSpoilageInfo)
    {
        //1.获取原始销售数量
        FruitsSpoilageInfo old = fruitsSpoilageInfoMapper.selectFruitsSpoilageInfoById(fruitsSpoilageInfo.getId());
        BigDecimal oldLossNum = old.getSpoilageNum();
        fruitsSpoilageInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsSpoilageInfo.setUpdateTime(DateUtils.getNowDate());
        int result = fruitsSpoilageInfoMapper.updateFruitsSpoilageInfo(fruitsSpoilageInfo);
        if(result>0)
        {
            //减掉库存
            FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(fruitsSpoilageInfo.getFruitsId());
            if(StringUtils.isNotNull(fruitsInfo))
            {
                //先把原始的损耗数量减去，在加上最新的损耗数量
                BigDecimal lossNum = fruitsInfo.getLossNum().subtract(oldLossNum).add(fruitsSpoilageInfo.getSpoilageNum());
                BigDecimal inventory = fruitsInfo.getInventory().add(oldLossNum).subtract(fruitsSpoilageInfo.getSpoilageNum());
                fruitsInfo.setLossNum(lossNum);
                fruitsInfo.setInventory(inventory);
            }
            fruitsInfoService.updateFruitsInfo(fruitsInfo);
        }
        return result;
    }

    /**
     * 批量删除果店损耗管理
     *
     * @param ids 需要删除的果店损耗管理主键
     * @return 结果
     */
    @Override
    public int deleteFruitsSpoilageInfoByIds(Long[] ids)
    {
        int count =0;
                for(Long id:ids)
                {
                    updateFruitsSpoilageInfoDelFlag(id);
                    count++;
                }
                return count;
    }

    /**
     * 删除果店损耗管理信息
     *
     * @param id 果店损耗管理主键
     * @return 结果
     */
    @Override
    public int deleteFruitsSpoilageInfoById(Long id)
    {
        updateFruitsSpoilageInfoDelFlag(id);
        return 1;
    }
    
    @Transactional
    public void  updateFruitsSpoilageInfoDelFlag(Long id)
    {
        FruitsSpoilageInfo temp = fruitsSpoilageInfoMapper.selectFruitsSpoilageInfoById(id);
        temp.setDelFlag(DelFlagConst.DELETED);
        temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        temp.setUpdateTime(DateUtils.getNowDate());
        fruitsSpoilageInfoMapper.updateFruitsSpoilageInfo(temp);
        
        //+回库存
        FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(temp.getFruitsId());
        if(StringUtils.isNotNull(fruitsInfo))
        {
            //删除入库，要把当前的入库数量从总库存中删除
            BigDecimal lossNum = fruitsInfo.getLossNum().subtract(temp.getSpoilageNum());
            BigDecimal inventory = fruitsInfo.getInventory().add(temp.getSpoilageNum());
            fruitsInfo.setLossNum(lossNum);
            fruitsInfo.setInventory(inventory);
        }
        fruitsInfoService.updateFruitsInfo(fruitsInfo);
    }
}
